const connection = require("../app/database")

class UserService {
  async create(user) {
    const { name,account, password } = user;//从user中解构name和password；
    const statement = `INSERT INTO user (name,account,password) VALUES (?,?,?);`
    //编写SQL语句；待传值用？代替；
    const result = await connection.execute(statement, [name,account, password]);
    //把name，password传入SQL语句，并把插入后的状态传给result；
    return result[0];
    //返回状态给路由
    // 到---->user.controller.js查看下一步逻辑
  }

  async getUserByAccount(account) {
    const statement = `SELECT * FROM user WHERE account = ?;`
    const result = await connection.execute(statement, [account]);
    return result[0];
  }

  async getDetailByAccount(account){
    const statement = `SELECT user.name,detail.balance,detail.coupon,detail.integral FROM user
                      LEFT JOIN detail
                      ON user.account = detail.account
                      WHERE user.account = ?;`
    const result = await connection.execute(statement,[account]);
    return result[0]
  }
}


module.exports = new UserService();